home *** CD-ROM | disk | FTP | other *** search
- /* viewppm.m */
- /* written by : Jason R. Wilson */
- /* last modified : 2/20/93 */
- /* this package provides a simple .ppm file viewer. */
- /* It is provided to give users a way of viewing output */
- /* from the NeXtrad package */
-
- #import <appkit/Application.h>
- #import <appkit/Window.h>
- #import <appkit/View.h>
- #import <appkit/Menu.h>
- #import <appkit/NXBitmapImageRep.h>
- #import <appkit/graphics.h>
- #include <stdlib.h>
- #include <stdio.h>
-
-
- void DoWork(void)
- {
- id myWindow, myView, myMenu, myImage; /* instance variables */
- unsigned char *ScreenBuf; /* used to hold the data for the NXBitmapImageRep */
- int loop; /* simple loop counter */
- FILE *fp; /* file pointer to the ppm file */
- char filename[256]; /* the name of the file to be viewed */
- char foo[256]; /* a trash string used to read in P6 (assumed) from top of .ppm file */
- NXRect graphicsRect; /* holds the view window coordinates of the image */
- int width; /* the width in pixels of the image */
- int height;/* the height in pixels of the image */
- int maxValue; /* the maximum color value of the .ppm file (assumed to be 255) */
- unsigned char r,g,b; /* the red, green, and blue values corresponding to a pixel in the image */
-
- printf ("Please input name of .ppm file to be viewed:\n");
- scanf ("%s",filename);
-
- fp = fopen (filename,"r");
-
- /* read in colors */
-
- fscanf (fp,"%s",foo);
- fscanf (fp,"%d %d",&width,&height);
- fscanf (fp,"%d",&maxValue);
- fscanf (fp,"%c",&r);
- ScreenBuf = (unsigned char *)(malloc(width*height*3));
- for (loop = 0;loop < width*height*3;loop += 3)
- {
- fscanf (fp,"%c%c%c",&r,&g,&b);
- ScreenBuf[loop] = r;
- ScreenBuf[loop+1] = g;
- ScreenBuf[loop+2] = b;
- }
-
- /* set up window */
-
- NXSetRect(&graphicsRect,100.0,350.0,(NXCoord)width,(NXCoord)height);
- myWindow = [[Window alloc] initContent:&graphicsRect
- style:NX_TITLEDSTYLE
- backing:NX_BUFFERED
- buttonMask:NX_MINIATURIZEBUTTONMASK
- defer:NO];
- [myWindow setTitle:filename];
- [myWindow display];
-
- /* set up menus */
- myMenu = [[Menu alloc] initTitle:"viewppm"];
- [myMenu addItem:"Quit" action:@selector(terminate:)
- keyEquivalent:'q'];
- [myMenu sizeToFit];
- [NXApp setMainMenu:myMenu];
-
- /* set up object to hold and draw bitmap */
-
- myImage = [[NXBitmapImageRep alloc] initData:ScreenBuf
- pixelsWide:width
- pixelsHigh:height
- bitsPerSample:8
- samplesPerPixel:3
- hasAlpha:NO
- isPlanar:NO
- colorSpace:2
- bytesPerRow:width*3
- bitsPerPixel:24];
-
- [myImage draw];
-
-
- myView = [[View alloc] initFrame:&graphicsRect];
- [myView setOpaque:YES];
- [myWindow setContentView:myView];
- [myWindow makeKeyAndOrderFront:nil];
-
- }
-
- void main()
- {
- [Application new];
- DoWork();
- [NXApp run];
- [NXApp free];
- }
-